install.packages('https://cran.r-project.org/src/contrib/Archive/interflex/interflex_1.0.8.tar.gz', 
                 repos=NULL, 
                 type='source')


library(magrittr)
library(stringr)
library(plyr)
library(tidyverse)
library(interflex)
library(gridExtra)
library(readxl)


rm(list=ls())
home = 'C:/Users/jdt34/Dropbox/VNA_Responsiveness/Short Article/JOP-dataverse/'


paste0(home, 'Code/interflex-kernel-spillover.R') %>%
  source
paste0(home, 'Code/randomization-inference-marginal-effects.R') %>%
  source


permutation.results = paste0(home, 'Data/RI-analyses.Rds') %>%
  readRDS
experimental.results = paste0(home, 'Data/experimental-analyses.Rds') %>%
  readRDS


dv_survey = paste0(home, 'Data/survey-outcomes.xlsx') %>%
  read_xlsx %>%
  mutate(Treatment=factor(x=Treatment, 
                          levels=c('Control',
                                   'Citizen',
                                   'Firm')),
         Missing=as.integer(is.na(Q1)),
         CitizenXProp.Citizen=Citizen*Prop.Citizen, 
         FirmXProp.Firm=Firm*Prop.Firm) %>%
  subset(!is.na(Treatment))
class(dv_survey) = 'data.frame'


tmp = subset(dv_survey, !is.na(Q1)) %>%
  ggplot(aes(x=Prop.Citizen, fill=factor(Citizen))) +
  geom_histogram(color='black', bins=50) +
  scale_fill_manual(values=c('white','black'))
citizen.survey.histogram = layer_data(tmp)[,c('fill','xmin','xmax','ymin','ymax')] %>% 
  mutate(xmax=0.974*(xmax+abs(min(xmin))), xmin=0.974*(xmin+abs(min(xmin))), 
         ymin=ymin/160-1, ymax=ymax/160-1)


tmp = subset(dv_survey, !is.na(Q1)) %>%
  ggplot(aes(x=Prop.Firm, fill=factor(Firm))) +
  geom_histogram(color='black', bins=50) +
  scale_fill_manual(values=c('white','black'))
firm.survey.histogram = layer_data(tmp)[,c('fill','xmin','xmax','ymin','ymax')] %>%
  mutate(xmax=0.974*(xmax+abs(min(xmin))), xmin=0.974*(xmin+abs(min(xmin))),
         ymin=ymin/160-1, ymax=ymax/160-1)
rm(tmp)


fig_7_1_left = rimer(result='tab2col3', 
                     X='Citizen', 
                     Z='Prop.Citizen')
fig_7_1_right = rimer(result='tab2col3', 
                      X='Firm', 
                      Z='Prop.Firm')
fig_7_1 = grid.arrange(fig_7_1_left, fig_7_1_right, 
                       nrow=1)


ggsave(filename='figure-A07-1.png', 
       plot=fig_7_1, 
       path=paste0(home, 'Figures/'), 
       width=12, 
       height=3.75, 
       units='in')
ggsave(filename='figure-A07-1.eps', 
       plot=fig_7_1, 
       path=paste0(home, 'Figures/'), 
       width=12, 
       height=3.75, 
       units='in', 
       device=cairo_ps)
